import {fileDownload} from "@/api/file.ts";

const useDownload = () => {
    const downloadMethod = async (path: string, fileName: string) => {
        const response = await fileDownload(path)
        const url = (window as { URL: { createObjectURL: (response: Blob) => string } }).URL.createObjectURL(response)
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        a.click();
    }
    const batchDownload = (data: Array<{ path: string, fileName: string }>) => {
        if (data.length === 0) return
        data.forEach(async (item) => {
            await downloadMethod(item.path, item.fileName)
        })
    }
    // canvas下载为本地图片(base64)
    const canvasToImage = (base: string) => {
        const a = document.createElement('a')
        const event = new MouseEvent('click')
        a.download = '裁剪图片.png'
        a.href = base
        a.dispatchEvent(event)
    }
    return { downloadMethod, batchDownload, canvasToImage }
}

export default useDownload